//
//  LZMeFooterView.m
//  百思不得姐
//
//  Created by 郝庆 on 16/5/22.
//  Copyright © 2016年 haoqing. All rights reserved.
//

#import "LZMeFooterView.h"
#import "AFNetWorking.h"
#import "LZHTTPSessionManager.h"
#import "LZMeSquare.h"
#import "LZSquareButton.h"
#import <SafariServices/SafariServices.h>
#import <MJExtension.h>
#import "LZLoginRegisterViewController.h"

@implementation LZMeFooterView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        // 参数
        NSMutableDictionary *params = [NSMutableDictionary dictionary];
        params[@"a"] = @"square";
        params[@"c"] = @"topic";
        
        __weak typeof(self) weakSelf = self;
        
        [[LZHTTPSessionManager manager]GET:LZCommonURL parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
            // 字典数组 -> 模型数组
            NSArray *squares = [LZMeSquare mj_objectArrayWithKeyValuesArray:responseObject[@"square_list"]];
            // 布局方块
            [weakSelf createSquares:squares];
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            LZLog(@"请求失败--%@",error);
        }];
    }
    return self;
}

#pragma mark - 根据模型数据创建对应的控件方块
- (void)createSquares:(NSArray *)squares
{
    // 服务器上方块的个数
    NSUInteger count = squares.count;
    // 设置每行需要显示的方块个数
    NSInteger cols = 4;
    // 界面上要显示的行数
    NSInteger rows = (count + cols - 1) / cols;
    // 界面上要显示的方块个数
    NSInteger showCount = cols * rows;
    // 方块的尺寸
    CGFloat buttonW = self.lz_width / cols;
    CGFloat buttonH = buttonW;
    
    for (NSUInteger i = 0; i < showCount; i++) {
        // 创建按钮
        LZSquareButton *button = [LZSquareButton buttonWithType:UIButtonTypeCustom];
        
        if (i < count) {
            [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
            
            // 取出i位置对应的模型设置数据
            button.square = squares[i];
            
        }else {
            button.enabled = NO;
            button.adjustsImageWhenDisabled = NO;
        }
        
        // 设置按钮的位置
        CGFloat buttonX = buttonW * (i % cols);
        CGFloat buttonY = buttonH * (i / cols);
        button.frame = CGRectMake(buttonX, buttonY, buttonW, buttonH);
        [self addSubview:button];
        
        // 设置footer的高度 == 最后一个按钮的bottom(最大Y值)
        self.lz_height = self.subviews.lastObject.lz_bottom;
        
        // 设置tableView的contentSize
        UITableView *tableView = (UITableView *)self.superview;
        tableView.tableFooterView = self;
        [tableView reloadData]; // 重新刷新数据(会重新计算contentSize)
    }
}

- (void)buttonClick:(LZSquareButton *)button
{
    LZMeSquare *square = button.square;
    if ([square.url hasPrefix:@"http"]) {
        // 获取TabBar控制器
        UITabBarController *tabBarVc = (UITabBarController *)self.window.rootViewController;
        // 取得相对应的导航控制器
        UINavigationController *naVc = tabBarVc.selectedViewController;
        
        SFSafariViewController *safarVc = [[SFSafariViewController alloc] initWithURL:[NSURL URLWithString:square.url]];
        [naVc presentViewController:safarVc animated:YES completion:nil];
    } else {
        LZLoginRegisterViewController *loginVc = [[LZLoginRegisterViewController alloc] init];
        [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:loginVc animated:YES completion:nil];
    }
}

@end
